package com.elitel.frame.base.service.impl;

import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.base.service.ApplicationCodeService;
import com.elitel.frame.main.entity.AcApp;
import com.elitel.frame.main.dao.AcAppMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 访问控制-应用编码控制器
 *  @author hi_bj
 */
@Service
public class ApplicationCodeServiceImpl implements ApplicationCodeService {
    private String curName="admin";
    @Autowired
    AcAppMapper acAppMapper;

    @Override
    public PageInfo getDataPage(PageInfo pageInfo, AcApp acApp) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<AcApp> resultList = acAppMapper.getAll(acApp);
        PageInfo<AcApp> page = new PageInfo(resultList);
        return page;
    }

    @Override
    public List<AcApp> getDataList(AcApp acApp) {
        return acAppMapper.getAll(acApp);
    }

    @Override
    public int insert(AcApp acApp) {
        if (ObjectUtils.isEmpty(acApp.getAppId())){
            acApp.setAppId(UUIDUtil.randomUUID());
        }
        Date curDate = new Date();
        acApp.setCreatedUser(curName);
        acApp.setCreatedTime(curDate);
        acApp.setUpdatedUser(curName);
        acApp.setUpdatedTime(curDate);
        return acAppMapper.insertSelective(acApp);
    }

    @Override
    public int update(AcApp acApp) {
        Date curDate = new Date();
        acApp.setUpdatedUser(curName);
        acApp.setUpdatedTime(curDate);
        return acAppMapper.updateByPrimaryKeySelective(acApp);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(String appId) {
        if (appId.contains(",")) {
            String[] appIds = appId.split(",");
            int count = 0;
            for (String id : appIds) {
                count += acAppMapper.deleteByPrimaryKey(id);
            }
            return count;
        } else {
            return acAppMapper.deleteByPrimaryKey(appId);
        }
    }

    @Override
    public AcApp selectByPrimaryKey(String appId) {
        return acAppMapper.selectByPrimaryKey(appId);
    }

    @Override
    public AcApp selectByAppCode(String appCode) {
        return acAppMapper.selectByAppCode(appCode);
    }

}
